// pages/pay/moreAddress/index.js
import { getAddList } from '../../../request/module.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addList:[],
    refresh:false,
    chosedId:'',
    radio:'1'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getAddList();
    if(options){
      let itemId=options.id
      this.setData({
        chosedId:itemId
      })
    }
    

  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let refresh=this.data.refresh
    if(refresh){
      this.getAddList()
      this.setData({
        refresh:false
      })
    }
  },
  getAddList(){
    getAddList().then((res)=>{
      let addList=res.extend.addressList
      addList.forEach(item=>{
        item.id=item.id + ''
        console.log(typeof(item.id))
      })
      this.setData({
        addList:addList
      })
    })
  },
  onChange1(event) {
    this.setData({
      radio: event.detail,
    });
  },
  insertAdd(){
    wx.navigateTo({
      url: '/pages/pay/moreAddress/inserAddress/index',
    })
  },
  onChange(e){
    console.log(e)
    this.setData({
      chosedId: e.detail,
    });
    console.log(typeof(this.data.chosedId))
    let address={}
    this.data.addList.forEach(item=>{
      if(item.id==this.data.chosedId){
        address=item
      }
    })
    console.log(address)
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面
    prevPage.setData({
      address:address
    }) 
    wx.navigateBack({
      delta: 1,
    })
  },

  

  
})